//
//  DataModel.m
//  WeiDuYY
//
//  Created by 王克杨的Mac on 2019/1/28.
//  Copyright © 2019 W ke杨. All rights reserved.
//

#import "DataModel.h"
#import "FMDB.h"
#import "LGModel.h"
static DataModel *data=nil;
@interface DataModel()
@property(nonatomic,strong)FMDatabase *fMDB;  //
@end

@implementation DataModel
+(DataModel *)sharedatamang{
    
    if (data == nil) {
        data = [[DataModel alloc] init];
        
    }
    return data;
    
}
+(instancetype)allocWithZone:(struct _NSZone *)zone
{
    if (data == nil) {
        data = [super allocWithZone:zone];
    }
    return data;
}

-(FMDatabase *)fMDB
{
    if (!_fMDB) {
        NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"movies.sqlite"];
        
        _fMDB = [FMDatabase databaseWithPath:path];
        [self initTable];
    }
    return _fMDB;
}

// 初始化数据表
-(void)initTable
{
    [_fMDB open];
    
    [_fMDB executeUpdate:@"CREATE TABLE IF NOT EXISTS movie (id INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT)"];
    
    [_fMDB close];
}

//-(void)addOneMovie:(ImgDModel *)movie
//{
//    [self.fMDB open];
//
//    // [self.fMDB executeUpdate:@"insert into movie (name,actor,date,price) values(?,?,?,?)",movie.name,movie.actor ,movie.date ,movie.price];
//
//    //    [self.fMDB executeUpdateWithFormat:@"insert into movie (title) values (%@)", movie.title];
//    //    BOOL success = [self.fMDB executeUpdateWithFormat:@"INSERT INTO movie (title) VALUES (%@)", @"Gus"];
//    //    BOOL success = [self.fMDB executeUpdateWithFormat:@"INSERT INTO movie (title) VALUES (Gus)"];
//
//    BOOL success =  [self.fMDB executeUpdate:@"insert into movie (title) values(?)",movie.title];
//    [self.fMDB close];
//}

//-(NSArray *)getAllMovies
//{
//    [self.fMDB open];
//
//    NSMutableArray *arr = [[NSMutableArray alloc] init];
//
//    FMResultSet *result = [self.fMDB executeQuery:@"SELECT * FROM movie"];
//    while ([result next])
//    {
//        ImgModel *movie = [[ImgModel alloc] init];
//        [arr addObject:movie];
//
//        movie.ID = [result intForColumnIndex:0];
//        movie.title = [result stringForColumnIndex:1];
//    }
//    [self.fMDB close];
//
//    return [arr copy];
//}
@end
